package com.bootdo.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bootdo.common.dao.SysDictMapper;
import com.bootdo.common.domain.SysDictDO;
import com.bootdo.system.dao.CourseMapper;
import com.bootdo.system.dao.CourseTimeMapper;
import com.bootdo.system.dao.CustomerMapper;
import com.bootdo.system.domain.Course;
import com.bootdo.system.domain.CourseTime;
import com.bootdo.system.domain.Customer;
import com.bootdo.system.service.CourseService;
import com.bootdo.system.service.UserService;
import com.github.pagehelper.PageHelper;

@Transactional
@Service("courseService")
public class CourseServiceImp implements CourseService {
	@Autowired
	CourseMapper courseMapper;
	
	@Autowired
	CourseTimeMapper courseTimeMapper;
	
	@Autowired
	CustomerMapper customerMapper;
	
	@Autowired
	SysDictMapper sysDictMapper;
	
	private static final Logger logger = LoggerFactory.getLogger(UserService.class);

	@Override
	public Course get(Long id) {
		Course c = courseMapper.get(id);
		if(c != null) {
			c.setCourseTimes(courseTimeMapper.listTimeByCourseId(id));
			Customer ct = customerMapper.get(c.getCourseOwner());
			if(ct != null){
				c.setOwnerName(ct.getCustomerName());
			}
			SysDictDO sd = sysDictMapper.get(c.getCourseType());
			if(sd != null) {
				c.setCourseTypeName(sd.getValue());
			}
		}
		return c;
	}

	@Override
	public List<Map<String,Object>> list(Map<String, Object> map) {
		PageHelper.startPage(Integer.parseInt(map.get("pageNum").toString()),Integer.parseInt(map.get("pageSize").toString()));
		return courseMapper.list(map);
	}

	@Override
	public int count(Map<String, Object> map) {
		return courseMapper.count(map);
	}

	@Override
	public int save(Course c) {
		c.setCreateTime(new Date());
		try {
			int count = courseMapper.save(c);
		}catch(Exception e) {
			e.printStackTrace();
		}
		List<CourseTime> times = c.getCourseTimes();
		if (times!= null && times.size() > 0) {
			courseTimeMapper.removeByCourseId(c.getId());
			for(CourseTime ct : times) {
				ct.setCourseId(c.getId());
				ct.setRemainderNumber(ct.getMaxBabys());
				ct.setSignedNumber(0);
				ct.setCourseStatus("0");
			}
			courseTimeMapper.batchSave(times);
		}
		return c.getId().intValue();
	}

	@Override
	public int update(Course c) {
		int r = courseMapper.update(c);
		List<CourseTime> times = c.getCourseTimes();
		if (times != null && times.size() > 0) {
			courseTimeMapper.removeByCourseId(c.getId());
			for(CourseTime ct : times) {
				ct.setCourseId(c.getId());
				ct.setRemainderNumber(ct.getMaxBabys());
				ct.setSignedNumber(0);
				ct.setCourseStatus("0");
			}
			courseTimeMapper.batchSave(times);
		}
		return r;
	}

	@Override
	public int remove(Long id) {
		return courseMapper.remove(id);
	}

	@Override
	public boolean exit(Map<String, Object> params) {
		boolean exit;
		exit = courseMapper.list(params).size() > 0;
		return exit;
	}
	
	@Override
	public int batchremove(List<Long> userIds) {
		int count = courseMapper.batchRemove(userIds);
		return count;
	}

	@Override
	public List<Map<String, Object>> wxList(Map<String, Object> map) {
		PageHelper.startPage(Integer.parseInt(map.get("pageNum").toString()),Integer.parseInt(map.get("pageSize").toString()));
		return courseMapper.wxList(map);
	}

	@Override
	public List<Map<String, Object>> recommendList(Map<String, Object> map) {
		PageHelper.startPage(Integer.parseInt(map.get("pageNum").toString()),Integer.parseInt(map.get("pageSize").toString()));
		return courseMapper.recommendList(map);
	}

	@Override
	public int updateStatus(Course c) {
		return courseMapper.updateStatus(c);
	}
}
